package com.inspectortime.webapp.inspector;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import com.inspectortime.domain.InspectorCompany;
import com.inspectortime.repository.Repository;
import com.inspectortime.webapp.util.HttpSessionUtils;

public class InspectorCentralController implements Controller {

	private Repository repository;
	
	public void setRepository(Repository repository) {
		this.repository = repository;
	}

	public ModelAndView handleRequest(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		
		InspectorCompany company = HttpSessionUtils.retrieveLoggedInCompany(request, repository);
		Calendar now = new GregorianCalendar();
		Calendar aYearAgo = (Calendar) now.clone();
		aYearAgo.add(Calendar.YEAR, -1); 
		List pastInspections = company.findInspectionsBetween(aYearAgo.getTime(), now.getTime());
		List futureInspections = company.findInspectionsBetween(new Date(), null);
		
		ModelAndView mv = new ModelAndView("inspector/central");
		mv.addObject("inspectorCompany", company);
		mv.addObject("pastInspections", pastInspections);
		mv.addObject("futureInspections", futureInspections);
		return mv;
	}

}
